﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.FrontEnd.DataContract;
using StockTrader.BusinessService.DataContract.Structure;
using StockTrader.BusinessService.DataContract.Hastc;
using StockTrader.BusinessService.DataContract;

namespace StockTrader.FrontEnd.OrderTracker.DAL
{
    public class DataAccess
    {
        private SortedList<int, SecuritiesInfo> stocks = new SortedList<int, SecuritiesInfo>();

        public List<OrderUI> getHoseOrders()
        {
            try
            {
                DALclient dac = new DALclient();
                List<OrderUI> list = new List<OrderUI>();

                List<OrderNewData> OrderNew = dac.getNewConditionedOrderData();
                List<OrderChangeData> OrderChange = dac.getOrderChangeData();
                List<OrderCancelData> OrderCancel = dac.getOrderCancelData();

                List<ConfirmOrderChangeData> ConfirmChange = dac.getConfirmOrderChangeData();
                List<ConfirmCrossingDealData> ConfirmCross = dac.getConfirmCrossingDealData();
                List<ConfirmDealData> ConfirmDeal = dac.getConfirmDealData();
                List<ConfirmOrderCancelData> ConfirmCancel = dac.getConfirmOrderCancelData();
                List<ConfirmOrderNewData> ConfirmNewOrder = dac.getConfirmOrderNewData();

                List<ConfirmDealData> cross = new List<ConfirmDealData>();
                foreach (ConfirmCrossingDealData cr in ConfirmCross)
                {
                    ConfirmDealData confirm = new ConfirmDealData();
                    confirm.TimeStamp = cr.Timestamp;
                    confirm.OrderNumber = cr.OrderNumberB;
                    confirm.Side = "B";
                    confirm.MatchedPrice = cr.Price;
                    confirm.MatchedVolume = cr.Volume;
                    confirm.CouterAccount = cr.OrderNumberS;
                    confirm.ConfirmNumber = cr.ConfirmNumber;
                    cross.Add(confirm);
                }
                foreach (ConfirmCrossingDealData cr in ConfirmCross)
                {
                    ConfirmDealData confirm = new ConfirmDealData();
                    confirm.TimeStamp = cr.Timestamp;
                    confirm.OrderNumber = cr.OrderNumberS;
                    confirm.Side = "S";
                    confirm.MatchedPrice = cr.Price;
                    confirm.MatchedVolume = cr.Volume;
                    confirm.CouterAccount = cr.OrderNumberB;
                    confirm.ConfirmNumber = cr.ConfirmNumber;
                    cross.Add(confirm);
                }


                foreach (OrderNewData order in OrderNew)
                {
                    OrderUI ui = new OrderUI();
                    List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                    List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();

                    ui.Time = order.TimeStamp.Substring(0, 2) + ":" + order.TimeStamp.Substring(2, 2) + ":" + order.TimeStamp.Substring(4, 2);
                    ui.OrderNo = order.OrderNumber;
                    ui.AccountNo = order.ClientID;
                    ui.Side = (order.Side.Equals("S")) ? "BÁN" : "MUA";
                    ui.Symbol = order.Symbol.Trim();
                    ui.Volume = order.Volume;
                    ui.Price = order.Price.Trim();
                    ui.Status = string.Empty;
                    ui.Market = "HOSE";

                    foreach (ConfirmDealData dl in ConfirmDeal)
                    {
                        if (dl.OrderNumber.Equals(order.OrderNumber))
                        {
                            OrderMatchedUI deal = new OrderMatchedUI();
                            deal.TimeStamp = dl.TimeStamp.Substring(0, 2) + ":" + dl.TimeStamp.Substring(2, 2) + ":" + dl.TimeStamp.Substring(4, 2);
                            deal.OrderNumber = dl.OrderNumber;
                            deal.ConfirmNumber = dl.ConfirmNumber;
                            deal.Volume = dl.MatchedVolume;
                            deal.Price = dl.MatchedPrice;
                            deal.MAccount = dl.CouterAccount;
                            dealList.Add(deal);
                        }
                    }

                    foreach (ConfirmDealData cdl in cross)
                    {
                        if (cdl.OrderNumber.Equals(order.OrderNumber))
                        {
                            OrderMatchedUI deal = new OrderMatchedUI();
                            deal.TimeStamp = cdl.TimeStamp.Substring(0, 2) + ":" + cdl.TimeStamp.Substring(2, 2) + ":" + cdl.TimeStamp.Substring(4, 2);
                            deal.OrderNumber = cdl.OrderNumber;
                            deal.ConfirmNumber = cdl.ConfirmNumber;
                            deal.Volume = cdl.MatchedVolume;
                            deal.Price = cdl.MatchedPrice;
                            deal.MAccount = cdl.CouterAccount;
                            dealList.Add(deal);
                        }
                    }

                    foreach (ConfirmOrderCancelData cnl in ConfirmCancel)
                        if (cnl.OrderNumber.Equals(order.OrderNumber))
                        {
                            ui.CancelDetail = new OrderCancelUI(cnl.Timestamp, cnl.OrderNumber, cnl.CancelShares, cnl.OrderCancelStatus);
                            if (cnl.OrderCancelStatus.Equals("S"))
                                ui.Status += "A";
                            else
                                ui.Status += "X";
                        }

                    foreach (ConfirmOrderChangeData chng in ConfirmChange)
                    {
                        if (chng.OrderNumber.Equals(order.OrderNumber))
                        {
                            changeList.Add(new HoseOrderChangeUI(chng.Timestamp, chng.OrderNumber, chng.ClientID, chng.PortClientFlag, chng.PublishedVolume, chng.Price));
                            ui.AccountNoChange = chng.ClientID;
                        }
                    }

                    if (changeList.Count > 0)
                        ui.Status += "M";

                    if (dealList.Count > 0)
                    {
                        foreach (OrderMatchedUI dl in dealList)
                        {
                            ui.MVolume += dl.Volume;
                            ui.MValue += dl.Volume * dl.Price * 1000;
                        }
                        if (ui.MVolume.Equals(ui.Volume))
                            ui.Status += (ui.Status.IndexOf("P") > -1) ? ui.Status.Replace("P", "F") : "F";
                        else
                            ui.Status += "P";
                    }

                    if (!string.IsNullOrEmpty(ui.AccountNoChange))
                        ui.AccountNo = ui.AccountNoChange;
                    ui.AveragePrice = (ui.MVolume.Equals(0)) ? 0 : ui.MValue / ui.MVolume / 1000;
                    ui.MatchedDetail = dealList;
                    ui.ChangeDetail = changeList;
                    list.Add(ui);
                }

                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }

        }

        public List<OrderUI> getHastcOrders()
        {
            try
            {
                DALclient dac = new DALclient();
                List<OrderUI> list = new List<OrderUI>();

                List<STSORDERS> stsOrder = dac.getSTS_ORDERS();
                List<STSTRADING_RESULT> stsResult = dac.getSTS_TRADING_RESULT();
                List<SecuritiesInfo> stockInfo = dac.getSTOCKSINFO();

                foreach (SecuritiesInfo item in stockInfo)
                    if (!stocks.ContainsKey(item.StockId))
                        stocks.Add(item.StockId, item);


                foreach (STSORDERS order in stsOrder)
                {
                    OrderUI ui = new OrderUI();
                    List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                    List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();

                    ui.AccountNo = order.ACCOUNT_NO;
                    ui.OrderNo = order.ORDER_NO;
                    ui.AccountNoChange = "";
                    ui.Status = "";
                    ui.Side = (order.OORB.Equals(1)) ? "MUA" : "BÁN";
                    ui.Symbol = stocks[Convert.ToInt32(order.STOCK_ID)].Symbol.Trim();
                    ui.Time = order.ORDER_TIME.Substring(0, order.ORDER_TIME.Length - 4);
                    ui.Volume = Convert.ToInt64(order.ORDER_QTTY);
                    ui.Price = (order.ORDER_PRICE / 1000).ToString("N1");
                    ui.Market = "HASTC";

                    if (!order.QUOTE_PRICE.Equals(0))
                        ui.PriceChange = (order.ORDER_PRICE.Equals(order.QUOTE_PRICE)) ? String.Empty : (order.QUOTE_PRICE / 1000).ToString("N1");
                    else
                    {
                        ui.PriceChange = string.Empty;
                        ui.Status += "X";
                    }


                    foreach (STSTRADING_RESULT rst in stsResult)
                    {
                        if (order.ORDER_NO.Equals(rst.B_ORDER_NO))
                            dealList.Add(new OrderMatchedUI(rst.MATCH_TIME.Substring(0, rst.MATCH_TIME.Length - 4), rst.B_ORDER_NO, rst.CONFIRM_NO, Convert.ToInt64(rst.QUANTITY), rst.PRICE / 1000, rst.S_ACCOUNT_NO));
                        else if (order.ORDER_NO.Equals(rst.S_ORDER_NO))
                            dealList.Add(new OrderMatchedUI(rst.MATCH_TIME.Substring(0, rst.MATCH_TIME.Length - 4), rst.S_ORDER_NO, rst.CONFIRM_NO, Convert.ToInt64(rst.QUANTITY), rst.PRICE / 1000, rst.B_ACCOUNT_NO));
                    }

                    if (dealList.Count > 0)
                        foreach (OrderMatchedUI dl in dealList)
                        {
                            ui.MVolume += dl.Volume;
                            ui.MValue += dl.Volume * dl.Price * 1000;
                        }

                    ui.AveragePrice = (ui.MVolume.Equals(0)) ? 0 : (ui.MValue / ui.MVolume / 1000);

                    if (!order.QUOTE_PRICE.Equals(0))
                        if (!order.ORDER_PRICE.Equals(order.QUOTE_PRICE))
                            ui.Status += "M";

                    if (ui.MVolume.Equals(ui.Volume))
                        ui.Status += (ui.Status.IndexOf("P") > -1) ? ui.Status.Replace("P", "F") : "F";
                    else if (ui.MVolume > 0)
                        ui.Status += "P";

                    ui.MatchedDetail = dealList;
                    list.Add(ui);
                }
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public List<OrderUI> getHosePtOrders()
        {
            try
            {
                List<OrderUI> list = new List<OrderUI>();
                DALclient dac = new DALclient();

                List<OFPTDealData> OFPT = dac.getOFPTDealData();
                List<TFPTDealData> TFPT = dac.getTFPTDealData();
                List<ConfirmPtDealData> ConfirmPtDeal = dac.getConfirmPtDealData();
                List<ConfirmPtDealCancelData> ConfirmPtDealCancel = dac.getConfirmPtDealCancelData();
                List<PtDealReplyData> PtReply = dac.getPtDealReplyData();
                List<PtACKData> PtACK = dac.getPtACKData();

//                 foreach (OFPTDealData ofpt in OFPT)
//                 {
//                     OrderUI ui = new OrderUI();
//                     List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
//                     List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();
// 
//                     ui.AccountNo = ofpt.ClientIDSeller;
//                     ui.Side = "BÁN";
//                     ui.Symbol = ofpt.SecuritySymbol.Trim();
//                     ui.Time = ofpt.Timestamp;
//                     ui.MVolume = ofpt.BrokerPortfolioVolBuyer + ofpt.BrokerClientVolBuyer + ofpt.MutualFundVolBuyer + ofpt.BrokerForeignVolBuyer;
// 
//                     dealList.Add(new OrderMatchedUI(ofpt.Timestamp, ofpt.deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, ofpt.ClientIDBuyer));
// 
//                     foreach (ConfirmPtDealCancelData cnl in ConfirmPtDealCancel)
//                     {
//                         if (cnl.ReplyCode.Equals("C") && cnl.ConfirmNumber.Equals(deal.ConfirmNumber))
//                         {
//                             ui.CancelDetail.Timestamp = cnl.Timestamp.Substring(0, 2) + ":" + cnl.Timestamp.Substring(2, 2) + ":" + cnl.Timestamp.Substring(4, 2);
//                             ui.CancelDetail.OrderCancelStatus = "C";
//                             ui.CancelDetail.CancelShares = Convert.ToInt64(deal.Volume);
//                             ui.CancelDetail.OrderNumber = deal.DealID;
//                         }
//                     }
//                     ui.MValue = ui.MVolume * ui.AveragePrice * 1000;
// 
//                     list.Add(ui);
//                 }

                foreach (ConfirmPtDealData deal in ConfirmPtDeal)
                {
                    OrderUI ui = new OrderUI();
                    List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                    List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();
                    List<OrderMatchedUI> dealList2 = new List<OrderMatchedUI>();

                    ui.OrderNo = deal.ConfirmNumber;
                    ui.Price = (Convert.ToDecimal(deal.Price) / 1000000).ToString("N1");
                    ui.Volume = Convert.ToInt64(deal.Volume);
                    ui.Time = deal.Timestamp.Substring(0, 2) + ":" + deal.Timestamp.Substring(2, 2) + ":" + deal.Timestamp.Substring(4, 2);

                    ui.MVolume = ui.Volume;
                    ui.AveragePrice = Convert.ToDecimal(deal.Price) / 1000000;
                    ui.MValue = ui.MVolume * ui.AveragePrice * 1000;

                    //                     ui.Side = (deal.Side.Equals("S")) ? "BÁN" : "MUA";
                    ui.Market = "HOSE";

                    foreach (OFPTDealData ofpt in OFPT)
                    {
                        if (ofpt.DealID.Equals(deal.DealID) && deal.Side.Trim().Equals("X"))
                        {
                            ui.AccountNo = ofpt.ClientIDSeller;
                            ui.Side = "BÁN";
                            ui.Symbol = ofpt.SecuritySymbol.Trim();
                            ui.Time = ofpt.Timestamp;

                            dealList.Add(new OrderMatchedUI(deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, ofpt.ClientIDBuyer));

                            foreach (ConfirmPtDealCancelData cnl in ConfirmPtDealCancel)
                            {
                                if (cnl.ReplyCode.Equals("C") && cnl.ConfirmNumber.Equals(deal.ConfirmNumber))
                                {
                                    ui.CancelDetail.Timestamp = cnl.Timestamp.Substring(0, 2) + ":" + cnl.Timestamp.Substring(2, 2) + ":" + cnl.Timestamp.Substring(4, 2);
                                    ui.CancelDetail.OrderCancelStatus = "C";
                                    ui.CancelDetail.CancelShares = Convert.ToInt64(deal.Volume);
                                    ui.CancelDetail.OrderNumber = deal.DealID;
                                }
                            }
                            ui.MValue = ui.MVolume * ui.AveragePrice * 1000;
                        }

                        if (ui.CancelDetail == null)
                            ui.Status = "F";
                        else
                            ui.Status = "X";

                        if (deal.Side.Trim().Equals("X"))
                        {
                            dealList2.Add(new OrderMatchedUI(deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, ofpt.ClientIDSeller));

                            ui.ChangeDetail = changeList;
                            ui.MatchedDetail = dealList2;

                            //list.Add(new OrderUI(ui.Time, ui.OrderNo, ofpt.ClientIDBuyer, ui.AccountNoChange, "MUA", ui.Symbol, ui.Volume, ui.Price, ui.Status, ui.PriceChange, ui.MVolume, ui.AveragePrice, ui.MValue, ui.ChangeDetail, dealList2, ui.CancelDetail, "HOSE"));
                            //break;
                        }
                    }


                    foreach (PtACKData ack in PtACK)
                    {
                        if (deal.ConfirmNumber.Equals(ack.ConfirmNumber))
                        {
                            OrderMatchedUI match = new OrderMatchedUI();
                            ui.Symbol = ack.SecuritySymbol.Trim();
                            ui.Side = "MUA";

                            match.ConfirmNumber = ack.ConfirmNumber;
                            match.MAccount = ack.ContraFirmS;
                            match.OrderNumber = ack.ConfirmNumber;
                            match.TimeStamp = ack.Timestamp.Substring(0, 2) + ":" + ack.Timestamp.Substring(2, 2) + ":" + ack.Timestamp.Substring(4, 2);
                            match.Price = Convert.ToDecimal(ack.Price) / 1000000;
                            match.Volume = Convert.ToInt64(ack.Volume);


                            foreach (PtDealReplyData reply in PtReply)
                            {
                                if (reply.ConfirmNumber.Equals(ack.ConfirmNumber))
                                {
                                    ui.AccountNo = reply.ClientIDB.Trim();
                                    if (reply.ReplyCode.Equals("C"))
                                        ui.Status = "X";
                                    else
                                        ui.Status = "F";
                                }
                            }
                            dealList.Add(match);
                        }
                    }


                    foreach (TFPTDealData tfpt in TFPT)
                    {
                        if (tfpt.DealID.Equals((deal.DealID)))
                        {
                            ui.AccountNo = tfpt.ClientIDSeller;
                            ui.Side = "BÁN";
                            ui.Symbol = tfpt.SecuritySymbol;
                            ui.Time = tfpt.TimeStamp;

                            dealList.Add(new OrderMatchedUI(deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, string.Empty));

                            foreach (ConfirmPtDealCancelData cnl in ConfirmPtDealCancel)
                            {
                                if (cnl.ReplyCode.Equals("C") && cnl.ConfirmNumber.Equals(deal.ConfirmNumber))
                                {
                                    ui.CancelDetail.Timestamp = cnl.Timestamp.Substring(0, 2) + ":" + cnl.Timestamp.Substring(2, 2) + ":" + cnl.Timestamp.Substring(4, 2);
                                    ui.CancelDetail.OrderCancelStatus = "C";
                                    ui.CancelDetail.CancelShares = Convert.ToInt64(deal.Volume);
                                    ui.CancelDetail.OrderNumber = deal.DealID;
                                }
                            }
                            ui.MValue = ui.MVolume * ui.AveragePrice * 1000;
                        }
                        if (ui.CancelDetail == null)
                            ui.Status = "F";
                        else
                            ui.Status = "X";

                    }

                    ui.MatchedDetail = dealList;
                    ui.ChangeDetail = changeList;
                    list.Add(ui);
                }

                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public List<OrderUI> getHastcPtOrders()
        {
            try
            {
                DALclient dac = new DALclient();
                List<OrderUI> list = new List<OrderUI>();

                List<STSTRADING_RESULT> stsResult = dac.getSTS_TRADING_RESULT();

                foreach (STSTRADING_RESULT rst in stsResult)
                {
                    if (rst.NORP.Equals(2))
                    {
                        OrderUI ui = new OrderUI();
                        List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                        List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();

                        ui.AccountNo = (rst.B_CODE_TRADE.Equals("59")) ? rst.B_ACCOUNT_NO : rst.S_ACCOUNT_NO;
                        ui.OrderNo = rst.S_ORDER_NO;
                        ui.AccountNoChange = "";
                        ui.Status = "F";
                        ui.Side = (rst.B_CODE_TRADE.Equals("59")) ? "MUA" : "BÁN";
                        ui.Symbol = rst.SEC_CODE.Trim();
                        ui.Time = rst.MATCH_TIME.Substring(0, rst.MATCH_TIME.Length - 4);
                        ui.Volume = Convert.ToInt64(rst.QUANTITY);
                        ui.Price = (rst.PRICE / 1000).ToString("N1");
                        ui.PriceChange = string.Empty;
                        ui.Market = "HASTC";
                        dealList.Add(new OrderMatchedUI(rst.MATCH_TIME, ui.OrderNo, rst.CONFIRM_NO, Convert.ToInt64(rst.QUANTITY), rst.PRICE / 1000, (rst.B_CODE_TRADE.Equals("59")) ? rst.S_ACCOUNT_NO : rst.B_ACCOUNT_NO));
                        ui.MVolume = Convert.ToInt64(rst.QUANTITY);
                        ui.MValue = rst.QUANTITY * rst.PRICE;
                        ui.AveragePrice = (ui.MVolume.Equals(0)) ? 0 : (ui.MValue / ui.MVolume / 1000);
                        ui.MatchedDetail = dealList;
                        list.Add(ui);
                    }
                }
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }

        }


        public List<OrderUI> getHoseOrdersSummary(DateTime from, DateTime to)
        {
            try
            {
                DALclient dac = new DALclient();
                List<OrderUI> list = new List<OrderUI>();

                List<OrderNewData> OrderNew = dac.getBackupNewConditionedOrderData(from, to);
                List<OrderChangeData> OrderChange = dac.getBackupOrderChangeData(from, to);
                List<OrderCancelData> OrderCancel = dac.getBackupOrderCancelData(from, to);

                List<ConfirmOrderChangeData> ConfirmChange = dac.getBackupConfirmOrderChangeData(from, to);
                List<ConfirmCrossingDealData> ConfirmCross = dac.getBackupConfirmCrossingDealData(from, to);
                List<ConfirmDealData> ConfirmDeal = dac.getBackupConfirmDealData(from, to);
                List<ConfirmOrderCancelData> ConfirmCancel = dac.getBackupConfirmOrderCancelData(from, to);

                List<ConfirmDealData> cross = new List<ConfirmDealData>();
                foreach (ConfirmCrossingDealData cr in ConfirmCross)
                {
                    ConfirmDealData confirm = new ConfirmDealData();
                    confirm.TimeStamp = cr.Timestamp;
                    confirm.OrderNumber = cr.OrderNumberB;
                    confirm.Side = "B";
                    confirm.MatchedPrice = cr.Price;
                    confirm.MatchedVolume = cr.Volume;
                    confirm.CouterAccount = cr.OrderNumberS;
                    confirm.ConfirmNumber = cr.ConfirmNumber;
                    cross.Add(confirm);
                }
                foreach (ConfirmCrossingDealData cr in ConfirmCross)
                {
                    ConfirmDealData confirm = new ConfirmDealData();
                    confirm.TimeStamp = cr.Timestamp;
                    confirm.OrderNumber = cr.OrderNumberS;
                    confirm.Side = "S";
                    confirm.MatchedPrice = cr.Price;
                    confirm.MatchedVolume = cr.Volume;
                    confirm.CouterAccount = cr.OrderNumberB;
                    confirm.ConfirmNumber = cr.ConfirmNumber;
                    cross.Add(confirm);
                }


                foreach (OrderNewData order in OrderNew)
                {
                    OrderUI ui = new OrderUI();
                    List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                    List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();

                    ui.Time = order.TimeStamp.Substring(0, 2) + ":" + order.TimeStamp.Substring(2, 2) + ":" + order.TimeStamp.Substring(4, 2);
                    ui.OrderNo = order.OrderNumber;
                    ui.AccountNo = order.ClientID;
                    ui.Side = (order.Side.Equals("S")) ? "BÁN" : "MUA";
                    ui.Symbol = order.Symbol.Trim();
                    ui.Volume = order.Volume;
                    ui.Price = order.Price.Trim();
                    ui.Status = string.Empty;
                    ui.Market = "HOSE";
                    ui.TransDate = order.TransDate;

                    foreach (ConfirmDealData dl in ConfirmDeal)
                    {
                        if (dl.OrderNumber.Equals(order.OrderNumber) && dl.TransDate.Equals(order.TransDate))
                        {
                            OrderMatchedUI deal = new OrderMatchedUI();
                            deal.TimeStamp = dl.TimeStamp.Substring(0, 2) + ":" + dl.TimeStamp.Substring(2, 2) + ":" + dl.TimeStamp.Substring(4, 2);
                            deal.OrderNumber = dl.OrderNumber;
                            deal.ConfirmNumber = dl.ConfirmNumber;
                            deal.Volume = dl.MatchedVolume;
                            deal.Price = dl.MatchedPrice;
                            deal.MAccount = dl.CouterAccount;
                            dealList.Add(deal);
                        }
                    }

                    foreach (ConfirmDealData cdl in cross)
                    {
                        if (cdl.OrderNumber.Equals(order.OrderNumber) && cdl.TransDate.Equals(order.TransDate))
                        {
                            OrderMatchedUI deal = new OrderMatchedUI();
                            deal.TimeStamp = cdl.TimeStamp.Substring(0, 2) + ":" + cdl.TimeStamp.Substring(2, 2) + ":" + cdl.TimeStamp.Substring(4, 2);
                            deal.OrderNumber = cdl.OrderNumber;
                            deal.ConfirmNumber = cdl.ConfirmNumber;
                            deal.Volume = cdl.MatchedVolume;
                            deal.Price = cdl.MatchedPrice;
                            deal.MAccount = cdl.CouterAccount;
                            dealList.Add(deal);
                        }
                    }

                    foreach (ConfirmOrderCancelData cnl in ConfirmCancel)
                        if (cnl.OrderNumber.Equals(order.OrderNumber) && cnl.TransDate.Equals(order.TransDate))
                        {
                            ui.CancelDetail = new OrderCancelUI(cnl.Timestamp, cnl.OrderNumber, cnl.CancelShares, cnl.OrderCancelStatus);
                            if (cnl.OrderCancelStatus.Equals("S"))
                                ui.Status += "A";
                            else
                                ui.Status += "X";
                        }

                    foreach (ConfirmOrderChangeData chng in ConfirmChange)
                    {
                        if (chng.OrderNumber.Equals(order.OrderNumber) && chng.TransDate.Equals(order.TransDate))
                        {
                            changeList.Add(new HoseOrderChangeUI(chng.Timestamp, chng.OrderNumber, chng.ClientID, chng.PortClientFlag, chng.PublishedVolume, chng.Price));
                            ui.AccountNoChange = chng.ClientID;
                        }
                    }

                    if (changeList.Count > 0)
                        ui.Status += "M";

                    if (dealList.Count > 0)
                    {
                        foreach (OrderMatchedUI dl in dealList)
                        {
                            ui.MVolume += dl.Volume;
                            ui.MValue += dl.Volume * dl.Price * 1000;
                        }
                        if (ui.MVolume.Equals(ui.Volume))
                            ui.Status += (ui.Status.IndexOf("P") > -1) ? ui.Status.Replace("P", "F") : "F";
                        else
                            ui.Status += "P";
                    }

                    if (!string.IsNullOrEmpty(ui.AccountNoChange))
                        ui.AccountNo = ui.AccountNoChange;

                    ui.AveragePrice = (ui.MVolume.Equals(0)) ? 0 : ui.MValue / ui.MVolume / 1000;
                    ui.MatchedDetail = dealList;
                    ui.ChangeDetail = changeList;
                    list.Add(ui);
                }

                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }

        }

        public List<OrderUI> getHastcOrdersSummary(DateTime from, DateTime to)
        {
            try
            {
                DALclient dac = new DALclient();
                List<OrderUI> list = new List<OrderUI>();

                List<STSORDERS> stsOrder = dac.getSTS_ORDERSBackup(from, to);
                List<STSTRADING_RESULT> stsResult = dac.getSTS_TRADING_RESULTBackup(from, to);
                List<SecuritiesInfo> stockInfo = dac.getSTOCKSINFOBackup(from, to);

                foreach (SecuritiesInfo item in stockInfo)
                    if (!stocks.ContainsKey(item.StockId))
                        stocks.Add(item.StockId, item);


                foreach (STSORDERS order in stsOrder)
                {
                    OrderUI ui = new OrderUI();
                    List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                    List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();

                    ui.AccountNo = order.ACCOUNT_NO;
                    ui.OrderNo = order.ORDER_NO;
                    ui.AccountNoChange = "";
                    ui.Status = "";
                    ui.Side = (order.OORB.Equals(1)) ? "MUA" : "BÁN";
                    ui.Symbol = stocks[Convert.ToInt32(order.STOCK_ID)].Symbol.Trim();
                    ui.Time = order.ORDER_TIME.Substring(0, order.ORDER_TIME.Length - 4);
                    ui.Volume = Convert.ToInt64(order.ORDER_QTTY);
                    ui.Price = (order.ORDER_PRICE / 1000).ToString("N1");
                    ui.Market = "HASTC";
                    ui.TransDate = order.ORDER_DATE;

                    if (!order.QUOTE_PRICE.Equals(0))
                        ui.PriceChange = (order.ORDER_PRICE.Equals(order.QUOTE_PRICE)) ? String.Empty : (order.QUOTE_PRICE / 1000).ToString("N1");
                    else
                    {
                        ui.PriceChange = string.Empty;
                        ui.Status += "X";
                    }


                    foreach (STSTRADING_RESULT rst in stsResult)
                    {
                        if (order.ORDER_NO.Equals(rst.B_ORDER_NO) && order.ORDER_DATE.Equals(rst.TRADING_DATE))
                            dealList.Add(new OrderMatchedUI(rst.MATCH_TIME.Substring(0, rst.MATCH_TIME.Length - 4), rst.B_ORDER_NO, rst.CONFIRM_NO, Convert.ToInt64(rst.QUANTITY), rst.PRICE / 1000, rst.S_ACCOUNT_NO));
                        else if (order.ORDER_NO.Equals(rst.S_ORDER_NO) && order.ORDER_DATE.Equals(rst.TRADING_DATE))
                            dealList.Add(new OrderMatchedUI(rst.MATCH_TIME.Substring(0, rst.MATCH_TIME.Length - 4), rst.S_ORDER_NO, rst.CONFIRM_NO, Convert.ToInt64(rst.QUANTITY), rst.PRICE / 1000, rst.B_ACCOUNT_NO));
                    }

                    if (dealList.Count > 0)
                        foreach (OrderMatchedUI dl in dealList)
                        {
                            ui.MVolume += dl.Volume;
                            ui.MValue += dl.Volume * dl.Price * 1000;
                        }

                    ui.AveragePrice = (ui.MVolume.Equals(0)) ? 0 : (ui.MValue / ui.MVolume / 1000);

                    if (!order.QUOTE_PRICE.Equals(0))
                        if (!order.ORDER_PRICE.Equals(order.QUOTE_PRICE))
                            ui.Status += "M";

                    if (ui.MVolume.Equals(ui.Volume))
                        ui.Status += (ui.Status.IndexOf("P") > -1) ? ui.Status.Replace("P", "F") : "F";
                    else if (ui.MVolume > 0)
                        ui.Status += "P";

                    ui.MatchedDetail = dealList;
                    list.Add(ui);
                }
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public List<OrderUI> getHosePtOrders(DateTime from, DateTime to)
        {
            try
            {
                List<OrderUI> list = new List<OrderUI>();
                DALclient dac = new DALclient();

                List<OFPTDealData> OFPT = dac.getOFPTDealData();
                List<TFPTDealData> TFPT = dac.getTFPTDealData();
                List<ConfirmPtDealData> ConfirmPtDeal = dac.getConfirmPtDealData();
                List<ConfirmPtDealCancelData> ConfirmPtDealCancel = dac.getConfirmPtDealCancelData();
                List<PtDealReplyData> PtReply = dac.getPtDealReplyData();
                List<PtACKData> PtACK = dac.getPtACKData();


                foreach (ConfirmPtDealData deal in ConfirmPtDeal)
                {
                    OrderUI ui = new OrderUI();
                    List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                    List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();
                    List<OrderMatchedUI> dealList2 = new List<OrderMatchedUI>();

                    ui.OrderNo = deal.ConfirmNumber;
                    ui.Price = (Convert.ToDecimal(deal.Price) / 1000000).ToString("N1");
                    ui.Volume = Convert.ToInt64(deal.Volume);
                    ui.Time = deal.Timestamp.Substring(0, 2) + ":" + deal.Timestamp.Substring(2, 2) + ":" + deal.Timestamp.Substring(4, 2);

                    ui.MVolume = ui.Volume;
                    ui.AveragePrice = Convert.ToDecimal(deal.Price) / 1000000;
                    ui.MValue = ui.MVolume * ui.AveragePrice * 1000;

                    //                     ui.Side = (deal.Side.Equals("S")) ? "BÁN" : "MUA";
                    ui.Market = "HOSE";

                    foreach (OFPTDealData ofpt in OFPT)
                    {
                        if (ofpt.DealID.Equals(deal.DealID) && deal.Side.Trim().Equals("X"))
                        {
                            ui.AccountNo = ofpt.ClientIDSeller;
                            ui.Side = "BÁN";
                            ui.Symbol = ofpt.SecuritySymbol.Trim();
                            ui.Time = ofpt.Timestamp;

                            dealList.Add(new OrderMatchedUI(deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, ofpt.ClientIDBuyer));

                            foreach (ConfirmPtDealCancelData cnl in ConfirmPtDealCancel)
                            {
                                if (cnl.ReplyCode.Equals("C") && cnl.ConfirmNumber.Equals(deal.ConfirmNumber))
                                {
                                    ui.CancelDetail.Timestamp = cnl.Timestamp.Substring(0, 2) + ":" + cnl.Timestamp.Substring(2, 2) + ":" + cnl.Timestamp.Substring(4, 2);
                                    ui.CancelDetail.OrderCancelStatus = "C";
                                    ui.CancelDetail.CancelShares = Convert.ToInt64(deal.Volume);
                                    ui.CancelDetail.OrderNumber = deal.DealID;
                                }
                            }
                            ui.MValue = ui.MVolume * ui.AveragePrice * 1000;
                        }

                        if (ui.CancelDetail == null)
                            ui.Status = "F";
                        else
                            ui.Status = "X";

                        if (deal.Side.Trim().Equals("X"))
                        {
                            dealList2.Add(new OrderMatchedUI(deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, ofpt.ClientIDSeller));

                            ui.ChangeDetail = changeList;
                            ui.MatchedDetail = dealList2;
                        }
                    }


                    foreach (PtACKData ack in PtACK)
                    {
                        if (deal.ConfirmNumber.Equals(ack.ConfirmNumber))
                        {
                            OrderMatchedUI match = new OrderMatchedUI();
                            ui.Symbol = ack.SecuritySymbol.Trim();
                            ui.Side = "MUA";

                            match.ConfirmNumber = ack.ConfirmNumber;
                            match.MAccount = ack.ContraFirmS;
                            match.OrderNumber = ack.ConfirmNumber;
                            match.TimeStamp = ack.Timestamp.Substring(0, 2) + ":" + ack.Timestamp.Substring(2, 2) + ":" + ack.Timestamp.Substring(4, 2);
                            match.Price = Convert.ToDecimal(ack.Price) / 1000000;
                            match.Volume = Convert.ToInt64(ack.Volume);


                            foreach (PtDealReplyData reply in PtReply)
                            {
                                if (reply.ConfirmNumber.Equals(ack.ConfirmNumber))
                                {
                                    ui.AccountNo = reply.ClientIDB.Trim();
                                    if (reply.ReplyCode.Equals("C"))
                                        ui.Status = "X";
                                    else
                                        ui.Status = "F";
                                }
                            }
                            dealList.Add(match);
                        }
                    }


                    foreach (TFPTDealData tfpt in TFPT)
                    {
                        if (tfpt.DealID.Equals((deal.DealID)))
                        {
                            ui.AccountNo = tfpt.ClientIDSeller;
                            ui.Side = "BÁN";
                            ui.Symbol = tfpt.SecuritySymbol;
                            ui.Time = tfpt.TimeStamp;

                            dealList.Add(new OrderMatchedUI(deal.Timestamp, deal.DealID, deal.ConfirmNumber, Convert.ToInt64(deal.Volume), Convert.ToDecimal(deal.Price) / 1000000, string.Empty));

                            foreach (ConfirmPtDealCancelData cnl in ConfirmPtDealCancel)
                            {
                                if (cnl.ReplyCode.Equals("C") && cnl.ConfirmNumber.Equals(deal.ConfirmNumber))
                                {
                                    ui.CancelDetail.Timestamp = cnl.Timestamp.Substring(0, 2) + ":" + cnl.Timestamp.Substring(2, 2) + ":" + cnl.Timestamp.Substring(4, 2);
                                    ui.CancelDetail.OrderCancelStatus = "C";
                                    ui.CancelDetail.CancelShares = Convert.ToInt64(deal.Volume);
                                    ui.CancelDetail.OrderNumber = deal.DealID;
                                }
                            }
                            ui.MValue = ui.MVolume * ui.AveragePrice * 1000;
                        }
                        if (ui.CancelDetail == null)
                            ui.Status = "F";
                        else
                            ui.Status = "X";

                    }

                    ui.MatchedDetail = dealList;
                    ui.ChangeDetail = changeList;
                    list.Add(ui);
                }

                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public List<OrderUI> getHastcPtOrders(DateTime from, DateTime to)
        {
            try
            {
                DALclient dac = new DALclient();
                List<OrderUI> list = new List<OrderUI>();

                List<STSTRADING_RESULT> stsResult = dac.getSTS_TRADING_RESULT();

                foreach (STSTRADING_RESULT rst in stsResult)
                {
                    if (rst.NORP.Equals(2))
                    {
                        OrderUI ui = new OrderUI();
                        List<OrderMatchedUI> dealList = new List<OrderMatchedUI>();
                        List<HoseOrderChangeUI> changeList = new List<HoseOrderChangeUI>();

                        ui.AccountNo = (rst.B_CODE_TRADE.Equals("59")) ? rst.B_ACCOUNT_NO : rst.S_ACCOUNT_NO;
                        ui.OrderNo = rst.S_ORDER_NO;
                        ui.AccountNoChange = "";
                        ui.Status = "F";
                        ui.Side = (rst.B_CODE_TRADE.Equals("59")) ? "MUA" : "BÁN";
                        ui.Symbol = rst.SEC_CODE.Trim();
                        ui.Time = rst.MATCH_TIME.Substring(0, rst.MATCH_TIME.Length - 4);
                        ui.Volume = Convert.ToInt64(rst.QUANTITY);
                        ui.Price = (rst.PRICE / 1000).ToString("N1");
                        ui.PriceChange = string.Empty;
                        ui.Market = "HASTC";
                        dealList.Add(new OrderMatchedUI(rst.MATCH_TIME, ui.OrderNo, rst.CONFIRM_NO, Convert.ToInt64(rst.QUANTITY), rst.PRICE / 1000, (rst.B_CODE_TRADE.Equals("59")) ? rst.S_ACCOUNT_NO : rst.B_ACCOUNT_NO));
                        ui.MVolume = Convert.ToInt64(rst.QUANTITY);
                        ui.MValue = rst.QUANTITY * rst.PRICE;
                        ui.AveragePrice = (ui.MVolume.Equals(0)) ? 0 : (ui.MValue / ui.MVolume / 1000);
                        ui.MatchedDetail = dealList;
                        list.Add(ui);
                    }
                }
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }

        }
    }
}
